package com.wolf.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author sdyang
 * @date 2019/10/12 8:58
 **/
public class StringUtils {

    public static final String EMPTY = "";

    public static boolean isEmpty(Object string) {
        String str = String.valueOf(string).trim();
        if (str == null || "".equals(str) || str.equals("null") || str.length() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 首字母小写
     *
     * @param str
     * @return
     * @date 2016-12-04
     */
    public static String firstLetterLowerCase(String str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1, str.length());
    }


    public static String getFullyQualifiedClassName() {
        StringBuffer sb = new StringBuffer();
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        int stacksLen = stacks.length;
        sb.append(stacks[1].getClassName());
        return sb.toString();
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public static String firstLetterUpperCase(String str) {

        //先全部转换为小写
        str = str.toLowerCase();

        //首字母大写
        char[] cs = str.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 该方法用来产生一个时间字符串（即：时间戳）
     *
     * @return
     */
    public static String getTimeStamp() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd hh:MM:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

    public static String getCurrentTimeMillis() {
        return String.valueOf(System.currentTimeMillis());
    }

    public static boolean isEmptyJSONArray(String str) {
        if (isEmpty(str)) {
            return true;
        }

        if ("[]".equals(str.trim())) {
            return true;
        }
        return false;
    }

    public static boolean isEmptyJson(String json) {
        if (isEmpty(json)
                || "{}".equals(json.trim())) {
            return true;
        }
        return false;
    }


    /**
     * 给第字符串第一个字母大写
     *
     * @param str
     * @return
     */
    public static String capitalize(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }
        return new StringBuilder(strLen)
                .append(Character.toTitleCase(str.charAt(0)))
                .append(str.substring(1))
                .toString();
    }

    public static String decapitalize(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }
        return new StringBuilder(strLen)
                .append(Character.toLowerCase(str.charAt(0)))
                .append(str.substring(1))
                .toString();
    }

    public static String camel(String str) {
        StringBuffer chars = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            //方法一
            if (Character.isUpperCase(str.charAt(i))) {
                // Complete this case
                chars.append('_');
            }
            chars.append(str.charAt(i));
        }
        return chars.toString().toLowerCase();
    }

    public static String thizMethodName() {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        String sql = String.format("%s.%s", stackTraceElements[2].getClassName(),
                stackTraceElements[2].getMethodName());
        return sql;
    }

    /**
     * 验证 字符串是否已经某个字符开头
     *
     * @param str
     * @param charStr
     */
    public static boolean startsWithStr(String str, String charStr) {
        if (!str.trim().startsWith(charStr) && !str.trim().startsWith(charStr.toUpperCase())) {
            return false;
        }
        return true;
    }

    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    public static boolean isDateType(String datevalue, String dateFormat) {
        if (isEmpty(datevalue)) {
            return false;
        }
        try {
            SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
            java.util.Date dd = fmt.parse(datevalue);
            if (datevalue.equals(fmt.format(dd))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }
}
